<?
/*
%%%copyright%%%
 * phpMyTicket - ticket reservation system
 * Copyright (C) 2004-2005 Anna Putrino, Stanislav Chachkov. All rights reserved.
 *
 * This file is part of phpMyTicket.
 *
 * This file may be distributed and/or modified under the terms of the
 * "GNU General Public License" version 2 as published by the Free
 * Software Foundation and appearing in the file LICENSE included in
 * the packaging of this file.
 *
 * Licencees holding a valid "phpmyticket professional licence" version 1
 * may use this file in accordance with the "phpmyticket professional licence"
 * version 1 Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 *
 * The "phpmyticket professional licence" version 1 is available at
 * http://www.phpmyticket.com/ and in the file
 * PROFESSIONAL_LICENCE included in the packaging of this file.
 * For pricing of this licence please contact us via e-mail to 
 * info@phpmyticket.com.
 * Further contact information is available at http://www.phpmyticket.com/
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Contact info@phpmyticket.com if any conditions of this licencing isn't 
 * clear to you.
 
 */
?>
<?

require_once("classes/ShopDB.php");
require_once("admin/AdminView.php");
require_once("classes/Organizer.php");

class PaymentView extends AdminView{
  function get_pay_modes (){
   // return array("cc"=>credit_card,"invoice"=>invoice);
   return array("invoice"=>invoice);
  }
  function get_send_modes (){
    return array("email"=>email,"post"=>post);
  }

function print_select_tpl ($name,&$data,&$err){
  global $_SHOP;

  $query="SELECT template_name FROM Template WHERE template_type='email' and template_organizer_id='{$_SHOP->organizer_id}' ORDER BY template_name";
  if(!$res=ShopDB::query($query)){
    return FALSE;
  }
  
  $sel[$data[$name]]=" selected ";

  echo "<tr><td class='admin_name'  width='40%'>".$this->con($name)."</td>
  <td class='admin_value'>
   <select name='$name'>
   <option value=''></option>\n";
  
  while($v=mysql_fetch_row($res)){
    $value=htmlentities($v[0],ENT_QUOTES);
    echo "<option value='$value' ".$sel[$v[0]].">{$v[0]}</option>\n";
  }
  
  echo "</select><span class='err'>{$err[$name]}</span>
  </td></tr>\n";
}

  function payment_check (&$data, &$err){
   global $_SHOP;
  // if(empty($data['payment_fee_fixe']) or empty($data['payment_fee_percent'])){$err['payment_fee']=mandatory;}
   if(empty($data['payment_email_template'])){$err['payment_email_template']=mandatory;}

   if($data["action"]=='insert'){
     $query="select count(*) from Payment where payment_mode='{$data['payment_mode']}'
        and payment_send='{$data['payment_send']}' and payment_organizer_id='{$_SHOP->organizer_id}'";
     $count=ShopDB::query_one_row($query);
     if($count[0]>0){
       $err['payment_mode']=payment_already_exist;
       $err['payment_send']=payment_already_exist;
     }
   }
   return empty($err);
  
  }
  
  function payment_form (&$data,&$err,$title){
  global $_SHOP;  
  echo "<form method='POST' action='{$_SERVER['PHP_SELF']}'>\n";
  echo "<table class='admin_form' width='500' cellspacing='1' cellpadding='4'>\n";
  echo "<tr><td class='admin_list_title' colspan='2'>".$title."</td></tr>"; 
  if($data['payment_id']){
    $pay=$this->get_pay_modes();
    $send=$this->get_send_modes();
    echo "<tr><td class='admin_name'>".$this->con(payment_mode)."</td>
    <td class='admin_value'>".$this->con($pay[$data['payment_mode']])."</td></tr>";
    echo "<tr><td class='admin_name'>".$this->con(payment_send)."</td>
    <td class='admin_value'>".$this->con($send[$data['payment_send']])."</td></tr>";
      
  }else{
    $sel[$data["payment_mode"]]=" selected ";
    echo "<tr><td class='admin_name'  width='40%'>".$this->con(payment_mode)."</td>
    <td class='admin_value'> <select name='payment_mode'>";
    $pay=$this->get_pay_modes();
    foreach($pay as $k=>$v){
       echo "<option value='$k' ".$sel[$k].">$v</option>\n";
    }
    echo "</select><span class='err'>{$err["payment_mode"]}</span></td></tr>\n";


    $sel[$data["payment_send"]]=" selected ";

    echo "<tr><td class='admin_name'  width='40%'>".$this->con(payment_send)."</td>
    <td class='admin_value'><select name='payment_send'>";
    $send=$this->get_send_modes();
    foreach($send as $k=>$v){
      echo "<option value='$k' ".$sel[$k].">$v</option>\n";
    }
    echo "</select><span class='err'>{$err["payment_send"]}</span></td></tr>\n";
  }
  $this->print_select_tpl('payment_email_template',$data,$err);

  $this->print_input('payment_fee_fixe',$data,$err,5,10);
  $this->print_input('payment_fee_percent',$data,$err,5,10);

  if($data['payment_id']){
    echo "<input type='hidden' name='payment_id' value='{$data['payment_id']}'/>\n";
    echo "<input type='hidden' name='action' value='update'/>\n";
  }else{
    echo "<input type='hidden' name='action' value='insert'/>\n";
  }
  echo "<tr><td align='center' class='admin_value' colspan='2'>
    <input type='submit' name='submit' value='".save."'>
  <input type='reset' name='reset' value='".res."'></td></tr>";
  echo "</table></form>\n";

  echo "<br><center><a class='link' href='{$_SERVER['PHP_SELF']}'>".admin_list."</a></center>";
  }
  function payment_list (){
  global $_SHOP;
  $query="select * from Payment  where  payment_organizer_id='{$_SHOP->organizer_id}' order by payment_mode";

  if(!$res=ShopDB::query($query)){
    return;
  }
  
  $alt=0;
  echo "<table class='admin_list' width='500' cellspacing='0' cellpadding='4'>\n";
  echo "<tr><td class='admin_list_title' colspan='8' align='center'>".payment_title."</td></tr>\n";
  $pay=$this->get_pay_modes();
  $send=$this->get_send_modes();
  while($row=mysql_fetch_assoc($res)){
     echo "<tr class='admin_list_row_$alt'>"; 
     echo  "<td  class='admin_list_item'>".$this->con($pay[$row['payment_mode']])."</td>";
     echo "<td class='admin_list_item'>".$this->con($send[$row['payment_send']])."</td>\n";
     echo "<td class='admin_list_item'>";
     $perc=$row['payment_fee_percent'];
     $fixe=$row['payment_fee_fixe'];
     if($perc > 0 ){
       echo $perc." % ";
     }
     if ($perc >0 and $fixe >0 ){
       echo "+";
     }
     if($fixe > 0){
       echo $fixe." ".$_SHOP->currency;
     }
     echo "</td>\n";
     echo "<td class='admin_list_item'>{$row['payment_email_template']}</td>\n";

     echo "<td class='admin_list_item' width='30'><a class='link' href='view_payment.php?action=edit&payment_id={$row['payment_id']}'><img src='images/edit.gif' border='0' alt='".edit."' title='".edit."'></a></td>\n";
     echo "<td class='admin_list_item' width='30'><a class='link' href='javascript:if(confirm(\"".delete_item."\")){location.href=\"view_payment.php?action=remove&payment_id={$row['payment_id']}\";}'><img src='images/trash.png' border='0' alt='".remove."' title='".remove."'></a></td>";
     
     echo "</tr>";
     $alt=($alt+1)%2;
   }

  echo "</table>\n";
  
  echo "<br><center><a class='link' href='{$_SERVER['PHP_SELF']}?action=add'>".add."</a></center>";
  }

  function draw (){
  global $_SHOP;
  if($_GET['action']=='remove' and $_GET['payment_id']>0){
    $query="DELETE FROM Payment WHERE payment_id='{$_GET['payment_id']}'  and payment_organizer_id='{$_SHOP->organizer_id}'";
    if(!ShopDB::query($query)){
      return 0;
    }
    $this->payment_list();
  }elseif($_GET['action']=='edit'){
    $query="SELECT * FROM Payment WHERE payment_id='{$_GET['payment_id']}' and payment_organizer_id='{$_SHOP->organizer_id}'";
    if(!$row=ShopDB::query_one_row($query)){
      return 0;
    }
    $this->payment_form($row,$err,payment_update_title);
  }else
  if($_POST['action']=='update'){
    if(!$this->payment_check($_POST,$err)){
      $this->payment_form($_POST,$err,payment_update_title);
      return 0;
    }    
    $query="UPDATE Payment SET 
    payment_fee_fixe='".$this->q($_POST['payment_fee_fixe'])."',
    payment_fee_percent='".$this->q($_POST['payment_fee_percent'])."',
    payment_email_template='".$this->q($_POST['payment_email_template'])."'
    WHERE payment_id='{$_POST['payment_id']}' and payment_organizer_id='{$_SHOP->organizer_id}'";
    
    
    if(!ShopDB::query($query)){
    echo mysql_error();
      return 0;
    }
    $this->payment_list();
  }elseif($_POST['action']=='insert'){
    if(!$this->payment_check($_POST,$err)){
      $this->payment_form($_POST,$err,payment_add_title);
    }else{
       $query="INSERT Payment (
       payment_organizer_id,
       payment_mode,
       payment_send,
       payment_fee_fixe,
       payment_fee_percent,
       payment_email_template
      )VALUES (
       '{$_SHOP->organizer_id}',
        '".$this->q($_POST['payment_mode'])."',
       '".$this->q($_POST['payment_send'])."',
       '".$this->q($_POST['payment_fee_fixe'])."',
       '".$this->q($_POST['payment_fee_percent'])."',
       '".$this->q($_POST['payment_email_template'])."')";
     
      if(!ShopDB::query($query)){
        return 0;
      }

      $payment_id=mysql_insert_id();
      $this->payment_list();
   }
  }elseif($_GET['action']=='add'){
    $this->payment_form($row,$err,payment_add_title);
  }else{
    $this->payment_list();
  }
}
}
?>
